<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.selectshopid" placeholder= "校区"  style="width:250px;height:50px">
          <!-- <el-option label="全部" value=""></el-option> -->
          <el-option v-for="(item, index) in shopids"
                     :label="item.shopName"
                     :key="item.shopName"
                     :value="item.id"
          ></el-option>
        </el-select>
        <!--<div style="float: right"><el-input style="width:150px;height:50px" v-model="dataForm.key" placeholder="课程名称" clearable></el-input></div>-->
      </el-form-item>
      <el-form-item>
        <!--<el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>-->
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('business:course:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('business:course:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button @click="download()" type="primary">导出模板</el-button>
      </el-form-item>
      <el-upload
        class="upload-demo"
        :on-preview="handlePreview"
        :action="url"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="successHandle"
        multiple
        :limit="1"
        :on-exceed="handleExceed">
        <el-button size="small" type="primary" v-bind:hidden="stuct">点击上传，批量导入课程信息</el-button>
      </el-upload>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="ID" width="80">
      </el-table-column>
      <el-table-column
        prop="shopName"
        header-align="center"
        align="center"
        label="校区名称">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="课程名字">
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="total"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程总节数">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="pic"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程配图">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="teacherid"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程老师编号">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="teacherName"
        header-align="center"
        align="center"
        label="老师姓名">
      </el-table-column>
	  <el-table-column
        prop="limitsNum"
        header-align="center"
        align="center"
        label="最大预约人数">
      </el-table-column>
      <!--
       <el-table-column
        prop="pic2"
        header-align="center"
        align="center"
        label="支付码">
		<template  slot-scope="scope">
		  <img :src="scope.row.pic2"  min-width="40" height="50" />
		</template>
      </el-table-column>
      -->
      <el-table-column
        prop="classPeriod"
        header-align="center"
        align="center"
        label="单节扣课时">
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="content"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程内容">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="begindate"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程开始日期">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="enddate"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="课程结束日期">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="courseweek"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="上课时间">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="remark"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="备注">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="createtime"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="新增时间">-->
      <!--</el-table-column>-->
      <!--<el-table-column-->
        <!--prop="updatetime"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="修改时间">-->
      <!--</el-table-column>-->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="informationHandle(scope.row.id)">详情</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <information v-if="informationVisible" ref="information" @refreshDataList="getDataList"></information>
  </div>
</template>

<script>
  import AddOrUpdate from './course-add-or-update'
  import Information from './course-information'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          selectshopid: ''
        },
        pic2: '',
        shopids: '',
        stuct: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        informationVisible: false,
        url: this.$http.adornUrl(`/business/excelImportCourse?token=${this.$cookie.get('token')}`)
      }
    },
    components: {
      AddOrUpdate,
      Information
    },
    activated () {
      this.getTeacherInfo()
    },
    methods: {
      // 获取老师的权限
      getTeacherInfo () {
        var userId = this.$store.state.user.id
        this.$http({
          url: this.$http.adornUrl('/business/student/queryTeacherLogin'),
          method: 'get',
          params: {'userId': userId}
        }).then(({data}) => {
          if (data.teacherInfo == null) {
            this.getDataList()
            this.getShopInfo()
            this.stuct = false
          } else {
            this.dataForm.selectshopid = data.teacherInfo.shopId
            this.getDataList()
            this.getShopInfo()
            this.stuct = true
          }
        })
      },
      // 获取分校信息-列表
      getShopInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'shopId': ''}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      // 获取数据列表
      getDataList (val) {
        this.dataListLoading = true
        if (val) {
          this.pageIndex = 1
        }
        this.$http({
          url: this.$http.adornUrl('/business/course/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'shopId': this.dataForm.selectshopid
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          // for (var i = 0; i < data.page.list.length; i++) {
          //   this.dataForm.shopid = data.page.list[i].shopid
          //   this.getShopName(i)
          // }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 详细
      informationHandle (id) {
        this.informationVisible = true
        this.$nextTick(() => {
          this.$refs.information.init(id)
        })
      },
      // getShopName (i) {
      //   this.$http({
      //     url: this.$http.adornUrl(`/business/membercard/getShopName`),
      //     method: 'get',
      //     params: {'id': this.dataForm.shopid}
      //   }).then(({data}) => {
      //     this.dataList[i].shopid = data
      //   })
      // },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/course/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      handleRemove (file, fileList) {
      },
      handlePreview (file) {
      },
      handleExceed (files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      beforeRemove (file, fileList) {
        return this.$confirm(`确定移除 ${file.name}?`)
      },
      successHandle (file, fileList) {
        console.log(file)
        if (file && file.code === 0) {
          this.$message({
            message: file.MSG,
            type: 'success',
            duration: 3000,
            onClose: () => {
              this.getDataList(1)
            }
          })
        } else {
          this.$message.error(file.MSG)
        }
      },
      download () {
        window.location.href = '../src/assets/课程表导入模板.xlsx'
      }
    }
  }
</script>
